package com.hui.account.activity;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.core.content.FileProvider;

import android.Manifest;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.AlertDialog;
import android.content.ContentUris;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.hui.account.R;
import com.hui.account.database.SQLManage;
import com.hui.account.dialog.CameraDialog;
import com.hui.account.dialog.DateDialog;
import com.hui.account.dialog.ImageChoiceDialog;
import com.hui.account.entity.FileEntity;
import com.hui.account.entity.RecordEntity;
import com.hui.account.entity.UserEntity;
import com.hui.account.utils.ButtonUtils;
import com.hui.account.utils.CommonUtils;
import com.hui.account.utils.ImageUtils;
import com.hui.account.utils.TimeUtils;
import com.hui.account.utils.ToastUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 修改欠款单
 */
public class RecordModActivity extends AppCompatActivity implements View.OnClickListener {

    String time;

    View lineView, inflate; // 竖线

    Uri imageUri; // 图片定位位置

    Context context;

    RecordEntity record;

    SharedPreferences preferences; // preferences：共享参数，存储图片

    CameraDialog dialog; // 上传图片弹框

    ImageButton delImageBtn;

    ImageView typeIcon, recordImage, payChange;

    LinearLayout dateLayout;

    RelativeLayout uploadLayout;

    TextView recordStatus, createTime, payTime, payText;

    EditText nameEdit, moneyEdit, remarkEdit;

    LinearLayout shootLayout, choiceLayout, cancelLayout; // 上传图片布局按键

    Button backBtn, delBtn, modBtn, refundBtn, finishBtn, save1Btn, save2Btn, line1Btn, line2Btn, line3Btn;

    @SuppressLint("SetTextI18n")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = getApplicationContext();
        setContentView(R.layout.activity_record_mod);

        // 1、设置屏幕顶部字体颜色
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);

        // 2、页面初始化
        initView();

        // 3、获取当前时间
        initTime();

        // 4、引入上传图片弹框
        initUploadImage();

        // 5、preferences：共享参数初始化，清空共享参数值
        try {
            boolean flag = true;
            preferences = getSharedPreferences("shareParam", Context.MODE_PRIVATE);
            SharedPreferences.Editor edit = preferences.edit();
            edit.putString("payMethod", "");
            edit.commit();

            String value = preferences.getString("selectRecord", "");
            if (!TextUtils.isEmpty(value)) {
                record = new Gson().fromJson(value, RecordEntity.class);
                UserEntity user = SQLManage.getUserById(record.getUserId());
                typeIcon.setImageResource(record.getIcon());
                nameEdit.setText(null == user ? "--" : user.getUserName());
                moneyEdit.setText(CommonUtils.MONEY + record.getMoney());
                createTime.setText(record.getTime());
                remarkEdit.setText(record.getRemark());
                payText.setText(record.getPay());
                payText.setVisibility(View.VISIBLE);
                payTime.setText(TimeUtils.getStringToTimestamp(record.getPayTime(), TimeUtils.TIME_SQL_PATTERN_TIME));
                if (record.getType() == 0) {
                    recordStatus.setText("未还");
                    recordStatus.setTextColor(context.getResources().getColor(R.color.red_FF5A5A));
                    moneyEdit.setTextColor(context.getResources().getColor(R.color.red_FF5A5A));
                } else {
                    modBtn.setVisibility(View.GONE);
                    lineView.setVisibility(View.GONE);
                    refundBtn.setVisibility(View.GONE);
                    finishBtn.setVisibility(View.VISIBLE);
                }

                if (!TextUtils.isEmpty(record.getImageName())) {
                    uploadLayout.setVisibility(View.GONE);
                    recordImage.setVisibility(View.VISIBLE);

                    // 根据图片名称查询图片路径
                    FileEntity file = SQLManage.getFileByName(record.getImageName());
                    if (null != file && !TextUtils.isEmpty(file.getPath())) {
                        Bitmap bitmap = ImageUtils.getDiskBitmap(file.getPath());
                        if (null != bitmap) {
                            recordImage.setImageBitmap(bitmap);
                            edit.putString("modImageName", record.getImageName());
                            edit.putString("modImageTemp", new Gson().toJson(bitmap));
                            edit.commit();
                            flag = false;
                        }
                    }
                    if (flag) {
                        edit.putString("modImageName", "");
                        edit.putString("modImageTemp", "");
                        edit.commit();
                    }
                }
            } else {
                ToastUtils.error(context, "欠款单信息获取失败", Gravity.TOP, 0, 15);
                finish();
            }
        } catch (Exception e) {
            Log.e(CommonUtils.TAG, "===> 【编辑欠款单】 页面加载" + e.getMessage());
        }
    }

    /**
     * 页面初始化
     */
    private void initView() {
        delBtn = findViewById(R.id.record_del);
        modBtn = findViewById(R.id.record_mod);
        backBtn = findViewById(R.id.record_mod_back);
        save1Btn = findViewById(R.id.record_mod_save_1);
        save2Btn = findViewById(R.id.record_mod_save_2);
        refundBtn = findViewById(R.id.record_refund);
        typeIcon = findViewById(R.id.record_mod_type_icon);
        payChange = findViewById(R.id.record_mod_pay_change);
        nameEdit = findViewById(R.id.record_mod_name);
        moneyEdit = findViewById(R.id.record_mod_money);
        payText = findViewById(R.id.record_mod_pay);
        createTime = findViewById(R.id.record_mod_create_time);
        payTime = findViewById(R.id.record_mod_pay_time);
        recordStatus = findViewById(R.id.record_mod_status);
        remarkEdit = findViewById(R.id.record_mod_remark);
        line1Btn = findViewById(R.id.record_mod_line_1);
        line2Btn = findViewById(R.id.record_mod_line_2);
        line3Btn = findViewById(R.id.record_mod_line_3);
        dateLayout = findViewById(R.id.record_mod_ll_5);
        lineView = findViewById(R.id.record_mod_line);
        finishBtn = findViewById(R.id.record_finish);
        delImageBtn = findViewById(R.id.record_mod_image_del);
        recordImage = findViewById(R.id.record_mod_image);
        uploadLayout = findViewById(R.id.record_mod_image_upload);

        backBtn.setOnClickListener(this);
        delBtn.setOnClickListener(this);
        modBtn.setOnClickListener(this);
        save1Btn.setOnClickListener(this);
        save2Btn.setOnClickListener(this);
        refundBtn.setOnClickListener(this);
        delImageBtn.setOnClickListener(this);
        uploadLayout.setOnClickListener(this);
        recordImage.setOnClickListener(this);
        payChange.setOnClickListener(this);
    }

    /**
     * Activity加载时执行
     */
    @Override
    protected void onResume() {
        super.onResume();
        // 设置付款方式
        SharedPreferences sharedPreferences = getSharedPreferences("shareParam", Context.MODE_PRIVATE);
        String value = sharedPreferences.getString("payMethod", "");
        if (!TextUtils.isEmpty(value)) {
            payText.setText(value);
            payText.setVisibility(View.VISIBLE);
            payChange.setVisibility(View.GONE);
        }
    }

    /**
     * 获取当前时间
     */
    private void initTime() {
        @SuppressLint("SimpleDateFormat") SimpleDateFormat format = new SimpleDateFormat(TimeUtils.TIME_SQL_PATTERN_TIME);
        time = format.format(new Date());
    }

    /**
     * 引入上传图片弹框
     */
    @SuppressLint("InflateParams")
    private void initUploadImage() {
        inflate = getLayoutInflater().inflate(R.layout.dialog_camera, null);
        shootLayout = findViewById(R.id.dialog_camera_shoot);
        choiceLayout = findViewById(R.id.dialog_camera_choice);
        cancelLayout = findViewById(R.id.dialog_camera_cancel);
    }

    /**
     * 逻辑删除欠款单
     */
    private void delRecord() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("确定删除").setMessage("是否确认删除该笔账单？").setNegativeButton("取消", null).setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                try {
                    // 1、保证逻辑删除欠款单仅有 100条
                    List<RecordEntity> list = SQLManage.getLogicDelRecords();
                    if (list.size() > 100) {
                        RecordEntity r = list.get(list.size() - 1);
                        if (!TextUtils.isEmpty(r.getImageName())) {
                            FileEntity file = SQLManage.getFileByName(r.getImageName());
                            if (null != file) {
                                SQLManage.delFile(file.getId());
                                ImageUtils.delDiskFile(file.getPath());
                            }
                        }
                        SQLManage.delRecord(r.getId());
                    }

                    // 2、逻辑删除欠款单
                    SQLManage.delLogicRecord(record.getId());
                    ToastUtils.success(context, "删除成功", Gravity.TOP, 0, 15);
                    finish();
                } catch (Exception e) {
                    Log.e(CommonUtils.TAG, "===> 【编辑欠款单】 逻辑删除欠款单" + e.getMessage());
                }
            }
        });
        builder.create().show();
    }

    /**
     * 选择欠款单还款时间
     */
    public void recordDate() {
        DateDialog dialog = new DateDialog(this);
        dialog.show();

        dialog.setOnEnsureListener(new DateDialog.OnEnsureListener() {
            @Override
            public void onEnsure(String time) {
                if (!TextUtils.isEmpty(time)) {
                    payTime.setText(time);
                }
            }
        });
    }

    /**
     * 校验是否有访问相机的权限
     */
    private void shootPhoto() {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
            // 1、拥有访问相机的权限，执行相机拍摄
            shoot();
        } else {
            // 2、动态申请相机权限
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, CommonUtils.REQUEST_CODE_SHOOT);
        }
    }

    /**
     * 动态申请相机权限后，接收方法
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == CommonUtils.REQUEST_CODE_SHOOT) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                shoot();
            } else {
                ToastUtils.error(context, "您已禁止访问相机，可前往设置更改", Gravity.CENTER, 0, 0);
            }
        } else if (requestCode == CommonUtils.REQUEST_CODE_CHOOSE) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                openPhoto();
            } else {
                ToastUtils.error(context, "您已禁止访问相册，可前往设置更改", Gravity.CENTER, 0, 0);
            }
        }
    }

    /**
     * 拍摄
     */
    private void shoot() {
        File imageTemp = new File(getExternalCacheDir(), "imageOut.jpeg");
        if (imageTemp.exists())
            imageTemp.delete();

        try {
            imageTemp.createNewFile();
        } catch (IOException e) {
            Log.e(CommonUtils.TAG, "RecordActivity shoot 异常信息:" + e.getMessage());
            return;
        }

        if (Build.VERSION.SDK_INT > 24) {
            imageUri = FileProvider.getUriForFile(this, "com.hui.account.file_upload", imageTemp);
        } else {
            imageUri = Uri.fromFile(imageTemp);
        }

        Intent intent = new Intent();
        intent.setAction("android.media.action.IMAGE_CAPTURE");
        intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
        startActivityForResult(intent, CommonUtils.REQUEST_CODE_SHOOT);
    }

    /**
     * 照片存储
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == CommonUtils.REQUEST_CODE_SHOOT && resultCode == RESULT_OK) {
            // 获取拍摄的照片
            try {
                InputStream input = getContentResolver().openInputStream(imageUri);
                Bitmap bitmap = BitmapFactory.decodeStream(input);
                display(bitmap);
            } catch (Exception e) {
                Log.e(CommonUtils.TAG, "RecordActivity onActivityResult 异常信息:" + e.getMessage());
            }
        } else if (requestCode == CommonUtils.REQUEST_CODE_CHOOSE) {
            if (Build.VERSION.SDK_INT < 19) {
                Uri uri = data.getData();
                String path = getImagePath(uri, null);
                if (null != path) {
                    Bitmap bitmap = BitmapFactory.decodeFile(path);
                    display(bitmap);
                }
            } else {
                getImageUpSDK19(data);
            }
        }
    }

    /**
     * SDK19以上获取图片
     */
    @TargetApi(19)
    private void getImageUpSDK19(Intent data) {
        String path = null;
        Uri uri = data.getData();
        if (DocumentsContract.isDocumentUri(this, uri)) {
            String documentId = DocumentsContract.getDocumentId(uri);
            if (TextUtils.equals(uri.getAuthority(), "com.android.providers.media.documents")) {
                String id = documentId.split(":")[1];
                String selection = MediaStore.Images.Media._ID + "=" + id;
                path = getImagePath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, selection);
            } else if (TextUtils.equals(uri.getAuthority(), "com.android.providers.downloads.documents")) {
                if (null != documentId && documentId.startsWith("msf:")) {
                    resolveMsf(uri); // 特殊处理
                    return;
                }
                Uri u = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(documentId));
                path = getImagePath(u, null);
            }
        } else if ("content".equalsIgnoreCase(uri.getScheme())) {
            path = getImagePath(uri, null);
        } else if ("file".equalsIgnoreCase(uri.getScheme())) {
            path = uri.getPath();
        }
        if (null != path) {
            Bitmap bitmap = BitmapFactory.decodeFile(path);
            display(bitmap);
        }
    }

    /**
     * 图片特殊处理
     */
    private void resolveMsf(Uri uri) {
        File file = new File(getCacheDir(), "temp_file" + getContentResolver().getType(uri).split("/")[1]);

        try {
            InputStream input = getContentResolver().openInputStream(uri);
            OutputStream out = new FileOutputStream(file);
            byte[] buffer = new byte[4 * 1024];
            int read;
            while ((read = input.read(buffer)) != -1) {
                out.write(buffer, 0, read);
            }
            out.flush();
            Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
            display(bitmap);
        } catch (Exception e) {
            Log.e(CommonUtils.TAG, "RecordActivity resolveMsf 异常信息:" + e.getMessage());
        }
    }

    /**
     * 获取图片路径
     */
    private String getImagePath(Uri uri, String selection) {
        String path = null;
        Cursor c = getContentResolver().query(uri, null, selection, null, null);
        if (null != c && c.moveToFirst()) {
            int index = c.getColumnIndex(MediaStore.Images.Media.DATA);
            path = c.getString(index);
            c.close();
        }
        return path;
    }

    /**
     * 显示图片
     */
    private void display(Bitmap bitmap) {
        // 1、展示照片
        uploadLayout.setVisibility(View.GONE);
        delImageBtn.setVisibility(View.VISIBLE);
        recordImage.setVisibility(View.VISIBLE);
        recordImage.setImageBitmap(bitmap);

        // 2、保存照片
        SharedPreferences.Editor edit = preferences.edit();
        edit.putString("modImageName", UUID.randomUUID().toString());
        edit.putString("modImageTemp", new Gson().toJson(bitmap));
        edit.commit();
    }

    /**
     * 从相册选择
     */
    private void choicePhoto() {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
            // 1、拥有访问相册的权限，打开相册
            openPhoto();
        } else {
            // 2、动态申请读取相册权限
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, CommonUtils.REQUEST_CODE_CHOOSE);
        }
    }

    /**
     * 打开相册
     */
    private void openPhoto() {
        Intent intent = new Intent("android.intent.action.GET_CONTENT");
        intent.setType("image/*");
        startActivityForResult(intent, CommonUtils.REQUEST_CODE_CHOOSE);
    }

    /**
     * 还款保存
     */
    private void refundRecord() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("确认还款").setMessage("是否确认欠款已结清？").setNegativeButton("取消", null).setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                modRecord(1);
            }
        });
        builder.create().show();
    }

    /**
     * 编辑保存
     *
     * @param flag 0=编辑 1=还款
     */
    private void modRecord(int flag) {
        if (0 == flag) {
            if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
                try {
                    String fileName = null;
                    String modImageName = preferences.getString("modImageName", "");
                    if (!modImageName.equals(record.getImageName())) {
                        String modImageTemp = preferences.getString("modImageTemp", "");
                        if (!TextUtils.isEmpty(modImageTemp)) {
                            // 欠款证明重新上传
                            fileName = System.currentTimeMillis() + "_0.png";
                            String finalFileName = fileName;
                            Bitmap bitmap = new Gson().fromJson(modImageTemp, Bitmap.class);
                            new Thread(new Runnable() {
                                @Override
                                public void run() {
                                    // 图片文件本地存储
                                    ImageUtils.saveBitmap(bitmap, finalFileName);

                                    // 添加文件记录
                                    SQLManage.addFile(new FileEntity(finalFileName, CommonUtils.IMAGES_PATH + finalFileName));
                                }
                            }).start();
                        }
                    }

                    if (null != fileName) {
                        // 欠款证明重新上传后，删除原图片
                        FileEntity file = SQLManage.getFileByName(record.getImageName());
                        if (null != file && !TextUtils.isEmpty(file.getPath()))
                            ImageUtils.delDiskFile(file.getPath());

                        record.setImageName(fileName);
                    }

                    String remarkValue = remarkEdit.getText().toString().trim();
                    record.setRemark(remarkValue);

                    SQLManage.modRecord(record);
                    ToastUtils.success(context, "编辑成功", Gravity.TOP, 0, 15);
                } catch (Exception e) {
                    Log.e(CommonUtils.TAG, "===> 【编辑欠款单】 编辑" + e.getMessage());
                }
            } else {
                //没有授权，编写申请权限代码
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, CommonUtils.REQUEST_CODE_WRITE);
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, CommonUtils.REQUEST_CODE_WRITE);
            }
        } else {
            try {
                String payValue = payText.getText().toString().trim();
                String payTimeValue = payTime.getText().toString().trim();

                if (TextUtils.isEmpty(payValue)) {
                    ToastUtils.error(context, "【必填项】 请输入付款方式！", Gravity.CENTER, 0, 0);
                    return;
                }

                if (TextUtils.isEmpty(payTimeValue)) {
                    ToastUtils.error(context, "【必填项】 请输入还款时间！", Gravity.CENTER, 0, 0);
                    return;
                }

                record.setType(1);
                record.setPay(payValue);
                record.setIcon(R.drawable.ic_already);

                try {
                    record.setPayTime(TimeUtils.getTimestampToString(payTimeValue, TimeUtils.TIME_SQL_PATTERN_TIME));
                } catch (ParseException e) {
                    Log.e(CommonUtils.TAG, "RecordModActivity 还款 异常信息:" + e.getMessage());
                    return;
                }

                SQLManage.refundRecord(record);
                ToastUtils.success(context, "还款成功", Gravity.TOP, 0, 15);
            } catch (Exception e) {
                Log.e(CommonUtils.TAG, "===> 【编辑欠款单】 还款" + e.getMessage());
            }
        }
        finish();
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.record_mod_back:
                finish();
                break;
            case R.id.record_del:
                if (!ButtonUtils.isFastDoubleClick()) {
                    delRecord(); // 逻辑删除欠款单
                }
                break;
            case R.id.record_mod:
                if (!ButtonUtils.isFastDoubleClick()) {
                    if (uploadLayout.getVisibility() == View.GONE)
                        delImageBtn.setVisibility(View.VISIBLE);

                    delBtn.setVisibility(View.GONE);
                    save1Btn.setVisibility(View.VISIBLE);
                    save2Btn.setVisibility(View.GONE);
                    line1Btn.setVisibility(View.GONE);
                    line2Btn.setVisibility(View.GONE);
                    line3Btn.setVisibility(View.VISIBLE);
                    remarkEdit.setEnabled(true);
                    payText.setVisibility(View.GONE);
                    payChange.setVisibility(View.GONE);
                    payText.setText("");
                    payTime.setText("");
                    dateLayout.setOnClickListener(null);
                    ToastUtils.success(context, "已进入编辑模式", Gravity.BOTTOM, 0, 200);
                }
                break;
            case R.id.record_refund:
                if (!ButtonUtils.isFastDoubleClick()) {
                    delImageBtn.setVisibility(View.GONE);
                    delBtn.setVisibility(View.GONE);
                    save1Btn.setVisibility(View.GONE);
                    save2Btn.setVisibility(View.VISIBLE);
                    line1Btn.setVisibility(View.VISIBLE);
                    line2Btn.setVisibility(View.VISIBLE);
                    line3Btn.setVisibility(View.GONE);
                    remarkEdit.setEnabled(false);
                    payChange.setVisibility(View.VISIBLE);
                    payTime.setText(time);
                    dateLayout.setOnClickListener(this);
                    ToastUtils.success(context, "已进入还款模式", Gravity.BOTTOM, 0, 200);
                }
                break;
            case R.id.record_mod_save_1:
                if (!ButtonUtils.isFastDoubleClick())
                    modRecord(0);

                break;
            case R.id.record_mod_save_2:
                if (!ButtonUtils.isFastDoubleClick())
                    refundRecord();

                break;
            case R.id.record_mod_ll_5:
                if (!ButtonUtils.isFastDoubleClick())
                    recordDate();

                break;
            case R.id.record_mod_image_del:
                if (!ButtonUtils.isFastDoubleClick()) {
                    uploadLayout.setVisibility(View.VISIBLE);
                    delImageBtn.setVisibility(View.GONE);
                    recordImage.setVisibility(View.GONE);

                    SharedPreferences.Editor edit = preferences.edit();
                    edit.putString("modImageName", "");
                    edit.putString("modImageTemp", "");
                    edit.commit();
                }
                break;
            case R.id.record_mod_image_upload:
                dialog = new CameraDialog(this);
                dialog.show();
                dialog.setDialogSize();
                break;
            case R.id.dialog_camera_shoot:
                shootPhoto();
                dialog.cancel();
                break;
            case R.id.dialog_camera_choice:
                choicePhoto();
                dialog.cancel();
                break;
            case R.id.dialog_camera_cancel:
                dialog.cancel();
                break;
            case R.id.record_mod_image:
                ImageChoiceDialog dialog = new ImageChoiceDialog(this);
                dialog.show();
                dialog.setDialogSize();
                break;
            case R.id.record_mod_pay_change:
                if (!ButtonUtils.isFastDoubleClick()) {
                    startActivity(new Intent(this, PayMethodActivity.class));
                }
                break;
        }
    }
}